package com.superm.controller;

import com.superm.entity.*;
import com.superm.service.*;
import com.superm.utils.EmpContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/retail")
public class RetailController {
    @Autowired
    private RelationShipService relationShipService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private OrderService orderService;

    /**
     * 跳转到商品列表页面
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("retail/allRetail");
        //查询全部的大类型
        List<RelationShip> relationShipList = relationShipService.selectPidMaxRelationShip();
        //查询全部品牌
        List<Brand> listBrand = brandService.selectLikeBrand(null, null, null);
        mv.addObject("listBrand", listBrand);
        mv.addObject("relationShipList", relationShipList);
        return mv;
    }
    /**
     * 模糊查询商品信息
     * @param goodsPoJo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/allRetail")
    @ResponseBody
    public Map<String,Object> allRetail(GoodsPoJo goodsPoJo, @RequestParam("page")Integer pageNo, @RequestParam("limit")Integer pageSize){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("success",true);
        goodsPoJo.setGoodsState("在售");
        //查询商品信息
        List<Goods> goodsList =  goodsService.selectLikeGoods(goodsPoJo,pageNo,pageSize);
        //获取总条数
        int count = goodsService.selectGoodsCount(goodsPoJo);
        map.put("data",goodsList);
        map.put("count",count);
        return map;
    }
    @RequestMapping("/purchase")//下单操作
    public ModelAndView purchase(Integer goodsId){
        Goods goods =new Goods();
        goods.setGoodsId(goodsId);
        ModelAndView mv = new ModelAndView("retail/addRetilOrder");
        goods =  goodsService.allGoods(goods);
        mv.addObject("goods",goods);
        return mv;
    }
    @RequestMapping("/addRetailOrder")
    @ResponseBody//添加一条暂存订单信息
    public Map<String,Object> allRetail(Order order){
        System.out.println("ssssssssssssssssssss");
        System.out.println(order);
        return orderService.addOrders(order);
    }
}
